package com.ch.light.mybatis.tools;

import com.ch.light.mybatis.pojo.DataSourceCfg;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;

import java.nio.charset.Charset;
import java.sql.*;
import java.util.Arrays;
import java.util.List;

/**
 * 描述：com.ch.light.mybatis.tools
 *
 * @author 80002023
 *         2017/5/13.
 * @version 1.0
 * @since 1.8
 */
public class SQLRunner {

    public static int exeFile(DataSourceCfg configure, String filePath) {
        try {
            Class.forName(configure.getDriverClass()).newInstance();
            Connection conn = DriverManager.getConnection(configure.getUrl(), configure.getUsername(), configure.getPassword());
            ScriptRunner runner = new ScriptRunner(conn);
            runner.setLogWriter(null);//设置是否输出日志
            Resources.setCharset(Charset.forName("GBK")); //设置字符集,不然中文乱码插入错误
            runner.runScript(Resources.getResourceAsReader(filePath));
            runner.closeConnection();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0;
    }

    public static int execute(DataSourceCfg configure, String sql) {
        Connection conn = null;
        try {
            Class.forName(configure.getDriverClass()).newInstance();
            conn = DriverManager.getConnection(configure.getUrl(), configure.getUsername(), configure.getPassword());
            conn.setAutoCommit(false);
            Statement statement = conn.createStatement();
            statement.execute(sql);
            conn.commit();
        } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
            rollback(conn);
        } finally {
            close(conn);
        }

        return 0;
    }

    public static int executeDDL(DataSourceCfg configure, String sql) {
        Connection conn = null;
        try {
            Class.forName(configure.getDriverClass()).newInstance();
            conn = DriverManager.getConnection(configure.getUrl(), configure.getUsername(), configure.getPassword());
            Statement statement = conn.createStatement();
            //  如果 execute的返回值是 ResultSet则返回 true
            boolean flag = statement.execute(sql);
            if (flag) { // 返回值是 ResultSet
                ResultSet rs = statement.getResultSet();
            } else {//
                int x = statement.getUpdateCount();
            }
        } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
            rollback(conn);
        } finally {
            close(conn);
        }

        return 0;
    }

    public static int exe(DataSourceCfg configure, List<String> sqlList) {
        Connection conn = null;
        try {
            Class.forName(configure.getDriverClass()).newInstance();
            conn = DriverManager.getConnection(configure.getUrl(), configure.getUsername(), configure.getPassword());
            conn.setAutoCommit(false);
            Statement statement = conn.createStatement();
            for (String sql : sqlList) {
                statement.addBatch(sql);
            }
            int[] rows = statement.executeBatch();
            System.out.println("Row count:" + Arrays.toString(rows));
            conn.commit();
        } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
            rollback(conn);
        } finally {
            close(conn);
        }

        return 0;
    }

    //DDL、DML、PROC、FUN、PKG、TRIG、EVENT、VIEW、JOB、ETL
    public static void exeDDL() {
    }

    public static void exeDML() {
    }

    public static void exeDCL() {
    }


    private static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void rollback(Connection conn) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }

}
